# coding=utf-8
from pprint import pprint

from py2neo import *

driver = Graph('bolt://119.91.156.232:7687', password='test')  # 认证连接数据库


def get_graph(name):
    data = {}
    entity = driver.run(
        f'''
        MATCH (n) WHERE n.name = '{name}' RETURN id(n), labels(n), n.name LIMIT 1
        ''').data()
    data['name'] = entity[0]['n.name']
    data['label'] = entity[0]['labels(n)'][0]
    data['id'] = entity[0]['id(n)']
    results = driver.run(
        f'''
        match path=(m)-[r]->(p) where p.name = '{name}' return id(p), id(m), labels(p), labels(m), p.name, m.name, type(r)
        ''').data()
    data['children'] = [
        {
            "id": i['id(m)'],
            "name": i['m.name'],
            "label": i['labels(m)'][0],
            "relationship": i['type(r)']
        }
        for i in results
    ]
    # pprint(results)
    return data


if __name__ == '__main__':
    pprint(get_graph(name='瓜果蔬菜'))
